#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2020/6/21 0021 11:22
# @Author  : wzy
# @Site    :
# @File    : run_main.py
# @Software: PyCharm
from Util.operate_excel import operateExcel
import json
from Util.operate_header import get_header,get_header_value
from Util.operate_result import operate_result,get_result_json,cmp_result_json
from Util.operate_cookie import get_cookie_value,write_cookie_value
from Util.data_codition import get_depend_data
from Base.base_request import request
#['imooc_001',
# '登陆',
# 'yes', None,
# 'login',
# 'post',
# '{"username":"111111"}',
# 'yes',
# 'message',
# None]

class RunMain:
    def run_case(self):
        # 获取用例总行数
        rows = operateExcel.get_rows()
        for i in range(rows):
            cookie=None
            get_cookie = None
            header = None
            depend_data = None
            data = operateExcel.get_rows_value(i+2)
            # 是否运行用例
            is_run = data[2]
            if is_run == 'yes':
                is_depend = data[3]
                data1 = json.loads(data[7])
                if is_depend:
                    '''
                    获取依赖数据
                    '''
                    depend_key = data[4]
                    depend_data = get_depend_data(is_depend)
                    print(depend_data)
                    data1[depend_key] = depend_data

                method = data[6]
                url = data[5]
                is_header = data[9]
                excepect_method = data[10]
                excepect_result = data[11]
                cookie_method = data[8]
                if cookie_method == 'yes':
                    cookie = get_cookie_value('is_cookie')
                if cookie_method == 'write':
                    '''
                    必须是获取到cookie
                    '''
                    get_cookie={"is_cookie":"is_cookie"}
                if is_header == 'yes':
                    header = get_header()
                res = request.run_main(method,url,data1,cookie,get_cookie,header)
                #print(res)
                code = str(res['errorCode'])
                message = res['errorDesc']
                if excepect_method == 'mec': 
                    config_message = operate_result(url,code)
                    if message == config_message:
                        operateExcel.excel_write_data(i+2,13,"通过")
                    else:
                        operateExcel.excel_write_data(i+2,13,"失败")
                        operateExcel.excel_write_data(i+2,14,json.dumps(res))
                if excepect_method == 'errorcode':
                    if excepect_result == code:
                        operateExcel.excel_write_data(i+2,14,"通过")
                    else:
                        operateExcel.excel_write_data(i+2,13,"失败")
                        operateExcel.excel_write_data(i+2,14,json.dumps(res))
                if excepect_method == 'json':
                    if code == 1000:
                        status_str='sucess'
                    else:
                        status_str='error'
                    excepect_result = get_result_json(url,status_str)
                    result = operate_result(res,excepect_result)
                    if result:
                        operateExcel.excel_write_data(i+2,13,"通过")
                    else:
                        operateExcel.excel_write_data(i+2,13,"失败")
                        operateExcel.excel_write_data(i+2,14,json.dumps(res))   
                
if __name__ == "__main__":
    # 不含数据依赖的runmain
    run = RunMain()
    run.run_case()
